package de.freudenreichdesign.webshop.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import de.freudenreichdesign.webshop.model.Adress;
import de.freudenreichdesign.webshop.model.Article;
import de.freudenreichdesign.webshop.model.Customer;
import de.freudenreichdesign.webshop.model.Order;
import de.freudenreichdesign.webshop.model.ShoppingCart;
import de.freudenreichdesign.webshop.model.ShoppingCartItem;
import de.freudenreichdesign.webshop.persistence.DatabaseUtil;

@Service("shoppingService")
public class ShoppingService {
	
	@Autowired private ShoppingCart shoppingCart;
	private static ShoppingService shoppingService = null;
	private DatabaseUtil databaseUtil = null;
	

	private ShoppingService() {
		databaseUtil = DatabaseUtil.getInstance();
	}
	
	public synchronized static ShoppingService getInstance() {
		if (shoppingService == null)
			shoppingService = new ShoppingService();
		return shoppingService;
	}
	
	public void clearShoppingCart() {
		shoppingCart.clearShoppingCart();
	}
	
	public void changeUnitsOfShoppingCartItem(int articleID, int units) {
		shoppingCart.getItemByArticleID(articleID).setUnits(units);
	}
	
	public void removeItemFromShoppingCart(int articleID) {
		shoppingCart.removeItemByArticleID(articleID);
	}
	
	public void addItemToShoppingCart(int articleID, int units) {
		ShoppingCartItem shoppingCartItem = shoppingCart.getItemByArticleID(articleID);
		if (shoppingCartItem == null) {
			Article article = ArticleService.getInstance().getArticleByID(articleID);
			ShoppingCartItem newShoppingCartItem = new ShoppingCartItem(article, units);
			shoppingCart.addItem(newShoppingCartItem);
		} else
			shoppingCartItem.setUnits(shoppingCartItem.getUnits() + units);
	}

	public void addItemToShoppingCart(int articleID) {
		ShoppingCartItem shoppingCartItem = shoppingCart.getItemByArticleID(articleID);
		if (shoppingCartItem == null) {
			Article article = ArticleService.getInstance().getArticleByID(articleID);
			ShoppingCartItem newShoppingCartItem = new ShoppingCartItem(article, 1);
			shoppingCart.addItem(newShoppingCartItem);
		} else
			shoppingCartItem.setUnits(shoppingCartItem.getUnits() + 1);
	}
	
	public void checkOutShoppingCart(long customerID, long deliveryAdressID, long invoiceAdressID) {
		//AccountService accountService = AccountService.getInstance();
		while (databaseUtil.startSaveTransaction() == false);
		Customer customer = (Customer)databaseUtil.loadObject(Customer.class.getName(), customerID);
		Adress deliveryAdress = (Adress)databaseUtil.loadObject(Adress.class.getName(), deliveryAdressID);
		Adress invoiceAdress;
		if (deliveryAdressID != invoiceAdressID)
			invoiceAdress = (Adress)databaseUtil.loadObject(Adress.class.getName(), invoiceAdressID);
		else
			invoiceAdress = deliveryAdress;
		//while (databaseUtil.startSaveTransaction() == false);		
		Order order = new Order(shoppingCart.getListOfArticles(), deliveryAdress, invoiceAdress, customer);
		databaseUtil.bufferBeforeSaveCommit(order);
		databaseUtil.saveCommit();
	}
	
	public ShoppingCart getShoppingCart() {
		return shoppingCart;
	}
}
